package com.ruoyi.web.controller.shop.goods;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.common.core.domain.ShopCategory;
import com.ruoyi.shop.goods.service.IShopBrandService;
import com.ruoyi.shop.goods.service.IShopGoodsCategoryService;

/**
 * 商品分类Controller
 *
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/goods/category")
public class ShopGoodsCategoryController extends BaseController {

    private String prefix = "shop/goods/category";

    @Autowired
    private IShopGoodsCategoryService shopGoodsCategoryService;
    @Autowired
    private IShopBrandService shopBrandService;

    @GetMapping()
    public String category() {
        return prefix + "/category";
    }

    @PostMapping("/list")
    @ResponseBody
    public List<ShopCategory> list(ShopCategory category) {
        List<ShopCategory> list = shopGoodsCategoryService.selectCategoryList(category);
        return list;
    }

    /**
     * 新增分类
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {
        ShopCategory category = new ShopCategory();
        if (parentId == 0) {
            category.setCategoryId(parentId);
            category.setCategoryName("无");
        } else {
            category = shopGoodsCategoryService.selectCategoryById(parentId);
        }
        mmap.put("category", category);
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("shop:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopCategory category) {
        category.setCreateBy(getLoginName());
        return toAjax(shopGoodsCategoryService.insertCategory(category));
    }

    /**
     * 修改商品分类
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
        ShopCategory category = shopGoodsCategoryService.selectCategoryById(categoryId);
        if (category.getParentId() == 0) {
            category.setParentName("无");
        }
        mmap.put("category", category);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("shop:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShopCategory category) {
        category.setUpdateBy(getLoginName());
        return toAjax(shopGoodsCategoryService.updateCategory(category));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("shop:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{categoryId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("categoryId") Long categoryId) {
        if (shopGoodsCategoryService.selectCategoryCount(categoryId) > 0) {
            return AjaxResult.warn("存在下级分类,不允许删除");
        }
//        if (deptService.checkDeptExistUser(deptId))
//        {
//            return AjaxResult.warn("分类存在用户,不允许删除");
//        }
        return toAjax(shopGoodsCategoryService.deleteCategoryById(categoryId));
    }

    /**
     * 选择分类树
     *
     * @param categoryId 分类ID
     * @param excludeId  排除ID
     */
    @GetMapping(value = {"/selectCategoryTree/{categoryId}", "/selectCategoryTree/{categoryId}/{excludeId}"})
    public String selectCategoryTree(@PathVariable("categoryId") Long categoryId,
                                     @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap) {
        ShopCategory category = new ShopCategory();
        if (categoryId == 0) {
            category.setCategoryId(categoryId);
            category.setCategoryName("无");
        } else {
            category = shopGoodsCategoryService.selectCategoryById(categoryId);
        }
        mmap.put("category", category);
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 加载分类列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
        ShopCategory category = new ShopCategory();
        category.setExcludeId(excludeId);
        List<Ztree> ztrees = shopGoodsCategoryService.selectCategoryTreeExcludeChild(category);
        return ztrees;
    }
    
    /**
     * 分类品牌关联页
     * 
     * @param categoryId
     * @param mmap
     * @return
     */
    @GetMapping("/bindBrand/{categoryId}")
    public String bindBrand(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
    	mmap.put("categoryId",categoryId);
    	mmap.put("brands", shopBrandService.selectBrandByCategoryId(categoryId));
        return prefix + "/bindBrand";
    }
    
	/**
	 * 保存分类品牌管理
	 * 
	 * @param brandIds
	 * @param categoryId
	 * @return
	 */
	@PostMapping("/bindBrand")
	@ResponseBody
	public AjaxResult bindBrand(String brandIds, Long categoryId) {
		return toAjax(shopBrandService.saveCategoryBrand(brandIds, categoryId));
	}

}
